import React from "react";
import { Input } from "antd";
import FormModal from "@/components/Form/Modal";
import { updatePassword } from "@/services/account";

export default ({ username = "admin", visible = true, toggleVisible }) => {
  const pwdRule = /* local.getPwdRule() */ {};
  const items = [
    {
      label: "用户名",
      name: "userName",
      rules: [{ required: true, message: "用户名不能为空!" }],
      value: username,
      render() {
        return <Input placeholder="请输入用户名" autoComplete="off" disabled />;
      },
    },
    {
      label: "旧密码",
      name: "oldPass",
      rules: [{ required: true, message: "旧密码不能为空!" }],
      render() {
        return (
          <Input.Password
            placeholder="请输入旧密码"
            autoComplete="new-password"
          />
        );
      },
    },
    {
      label: "新密码",
      name: "password",
      rules: [
        { required: true, message: "新密码不能为空!" },
        () => ({
          validator(rule, value) {
            const chineseReg = /[\u4e00-\u9fa5]/;
            const numberReg = /[0-9]/;
            const upperReg = /[A-Z]/;
            const lowerReg = /[a-z]/;
            const charReg = /[`~'"!@#$%^&*()~,.?/{}<>[\]]/;
            const {
              number,
              upper,
              lower,
              special_char: char,
            } = pwdRule.pwd_strength;
            if (chineseReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("不允许包含中文");
            }
            if (number === 1 && !numberReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含数字");
            }
            if (upper === 1 && !upperReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含大写字母");
            }
            if (lower === 1 && !lowerReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含小写字母");
            }
            if (char === 1 && !charReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含`~'\"!@#$%^&*()~,.?/{}<>[]");
            }
            if (
              value.length < pwdRule.pwd_shortest_length ||
              value.length > 50
            ) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject(
                `密码长度必须大于等于${pwdRule.pwd_shortest_length}, 小于50`
              );
            }
            // if (getFieldValue('password') !== value) {
            //   // eslint-disable-next-line prefer-promise-reject-errors
            //   return Promise.reject('两次密码不一致');
            // }
            // eslint-disable-next-line prefer-promise-reject-errors
            return Promise.resolve();
          },
        }),
      ],
      render() {
        return (
          <Input.Password
            placeholder="请输入新密码"
            autoComplete="new-password"
          />
        );
      },
    },
    {
      label: "确认密码",
      name: "confirmPass",
      rules: [
        { required: true, message: "确认密码不能为空!" },
        ({ getFieldValue }) => ({
          validator(rule, value) {
            const numberReg = /[0-9]/;
            const upperReg = /[A-Z]/;
            const lowerReg = /[a-z]/;
            const charReg = /[`~'"!@#$%^&*()~,.?/{}<>[\]]/;
            const {
              number,
              upper,
              lower,
              special_char: char,
            } = pwdRule.pwd_strength;
            if (number === 1 && !numberReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含数字");
            }
            if (upper === 1 && !upperReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含大写字母");
            }
            if (lower === 1 && !lowerReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含小写字母");
            }
            if (char === 1 && !charReg.test(value)) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("必须包含`~'\"!@#$%^&*()~,.?/{}<>[]");
            }
            if (
              value.length < pwdRule.pwd_shortest_length ||
              value.length > 50
            ) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject(
                `密码长度必须大于等于${pwdRule.pwd_shortest_length}, 小于50`
              );
            }
            if (getFieldValue("password") !== value) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return Promise.reject("两次密码不一致");
            }
            // eslint-disable-next-line prefer-promise-reject-errors
            return Promise.resolve();
          },
        }),
      ],
      render() {
        return (
          <Input.Password
            placeholder="请输入新密码"
            autoComplete="new-password"
          />
        );
      },
    },
  ];

  const confirm = (values) => {
    // TODO:
    toggleVisible()
    updatePassword(values)
  };

  return (
    <FormModal
      title="修改密码"
      visible={visible}
      items={items}
      onFinish={confirm}
      onCancel={toggleVisible}
    />
  );
};
